package com.clw.template.bo;

import com.clw.template.entity.SysAdmin;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class AdminUserDetails implements UserDetails {

    private SysAdmin sysAdmin;
    private List<String> permissionList;

    public AdminUserDetails(SysAdmin sysAdmin, List<String> permissionList) {
        this.sysAdmin = sysAdmin;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        return Arrays.asList(new SimpleGrantedAuthority("*"));
        return permissionList.stream()
                .filter(permission -> StringUtils.isNotEmpty(permission))
                .map(permission -> new SimpleGrantedAuthority(permission))
                .collect(Collectors.toList());

    }

    @Override
    public String getPassword() {
        return sysAdmin.getPassword();
    }

    @Override
    public String getUsername() {
        return sysAdmin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return  sysAdmin.getStatus().equals(1);
    }
}